﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public class LogHelper
    {
        static StackTrace trace = new StackTrace();
        /**
         * 向日志写入调试信息
         * @param className 类名
         * @param content 写入内容
         */
        public static void Debug(string content)
        { 
            WriteLog("DEBUG", content);
        } 
        /**
        * 向日志写入运行时信息
        * @param className 类名
        * @param content 写入内容
        */
        public static void Info(string content)
        {
            WriteLog("INFO", content);
        } 
        /**
        * 向日志写入出错信息
        * @param className 类名
        * @param content 写入内容
        */
        public static void Error(string content)
        {
            WriteLog("ERROR", content);
        } 
        /**
        * 实际的写日志操作
        * @param type 日志记录类型
        * @param className 类名
        * @param content 写入内容
        */
        protected static void WriteLog(string type, string content)
        { 
            string methodName = trace.GetFrame(2).GetMethod().Name;
            string className = trace.GetFrame(2).GetMethod().ReflectedType.FullName;
            string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");//获取当前系统时间
            //日志内容
            string write_content = time + " " + type + " 类【" + className + "】 方法【" + methodName + "】：" + content + "\r\n";
            //需要用户自定义日志实现形式
            File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "/logs\\" + type + ".txt", write_content);
            Console.WriteLine(write_content);
        }
        /// <summary>
        /// 自定义日志
        /// </summary>
        /// <param name="logName"></param>
        /// <param name="className"></param>
        /// <param name="methodName"></param>
        /// <param name="content"></param>
        public static void zdyWriteLog(string logName, string content)
        { 
            string methodName = trace.GetFrame(1).GetMethod().Name;
            string className = trace.GetFrame(1).GetMethod().ReflectedType.FullName;
            string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");//获取当前系统时间
            //日志内容
            string write_content = time + " 类【" + className + "】 方法【" + methodName + "】：" + content + "\r\n";
            //需要用户自定义日志实现形式
            File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "/logs\\" + logName + ".txt", write_content);
            Console.WriteLine(write_content);
        }
    }
}
